package com.ruoyi.storage.tickets.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.basicData.product.domain.Product;
import com.ruoyi.basicData.product.mapper.ProductMapper;
import com.ruoyi.basicData.ruleSetting.service.RuleSettingService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.outbound.consignment.mapper.ConsignmentMapper;
import com.ruoyi.storage.bicycle.domain.Bicycle;
import com.ruoyi.storage.bicycle.mapper.BicycleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.storage.tickets.mapper.TicketsMapper;
import com.ruoyi.storage.tickets.domain.Tickets;
import com.ruoyi.storage.tickets.service.ITicketsService;

/**
 * 生产工单Service业务层处理
 * 
 * @author zzx
 * @date 2024-01-24
 */
@Service
public class TicketsServiceImpl implements ITicketsService 
{
    @Autowired
    private TicketsMapper ticketsMapper;
    @Autowired
    private RuleSettingService ruleSettingService;
    @Autowired
    private BicycleMapper bicycleMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ConsignmentMapper consignmentMapper;

    /**
     * 查询生产工单
     * 
     * @param ticketsId 生产工单主键
     * @return 生产工单
     */
    @Override
    public Tickets selectTicketsByTicketsId(String ticketsId)
    {

        return ticketsMapper.selectTicketsByTicketsId(ticketsId);
    }

    /**
     * 查询生产工单列表
     * 
     * @param tickets 生产工单
     * @return 生产工单
     */
    @Override
    public List<Tickets> selectTicketsList(Tickets tickets)
    {
        List<Tickets> tickets1 = ticketsMapper.selectTicketsList(tickets);
        for (Tickets ticket :tickets1) {
            Long returnNumber=consignmentMapper.selectReturnNumberByClientOrderNumber(ticket.getClientOrderNumber());
            if (returnNumber!=null){
                ticket.setReturnNumber(returnNumber);
                Long quantityStorage = ticket.getQuantityStorage();
                ticket.setBalanceQuantity(quantityStorage-returnNumber);
            }
        }
        return tickets1;
    }

    /**
     * 新增生产工单
     * 
     * @param tickets 生产工单
     * @return 结果
     */
    @Override
    public int insertTickets(Tickets tickets)
    {
        String supplier_id = ruleSettingService.genDocId(Tickets.class, "TICkETS_ID");
        Product product = productMapper.selectProductByProductId(tickets.getProductId());
        System.out.println(product.getProductName());
        tickets.setTicketsId(supplier_id);
        tickets.setProductLine(null);
        tickets.setBillData(new Date());
        tickets.setAudit(0L);
        tickets.setProductName(product.getProductName());
        tickets.setClose(0L);
        tickets.setDelFlag(2L);
        tickets.setQuantityPacked(0L);
        tickets.setQuantityPackedNostored(0L);
        tickets.setQuantityStorage(0L);
        return ticketsMapper.insertTickets(tickets);
    }

    /**
     * 修改生产工单
     * 
     * @param tickets 生产工单
     * @return 结果
     */
    @Override
    public int updateTickets(Tickets tickets)
    {
        return ticketsMapper.updateTickets(tickets);
    }

    /**
     * 批量删除生产工单
     * 
     * @param ticketsIds 需要删除的生产工单主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteTicketsByTicketsIds(String[] ticketsIds)
    {
        Boolean b=false;
        for (String ticketsId:ticketsIds) {
            int audit = ticketsMapper.selectAuditByTicketsId(ticketsId);
            if (audit==2){
                b=true;
            }
        }
        if (!b){
            ticketsMapper.deleteTicketsByTicketsIds(ticketsIds);
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.error("删除失败，存在已审核工单");
        }

    }

    /**
     * 删除生产工单信息
     * 
     * @param ticketsId 生产工单主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteTicketsByTicketsId(String ticketsId)
    {
        int audit = ticketsMapper.selectAuditByTicketsId(ticketsId);
        if (audit==0){
            ticketsMapper.deleteTicketsByTicketsId(ticketsId);
            return AjaxResult.success("删除成功");
        }else {
            return AjaxResult.error("删除失败，已审核");
        }

    }

    /**
     * 审核/反审核生产工单
     *
     * @param ticketsId 生产工单主键
     * @return 结果
     */
    @Override
    public AjaxResult updateAudit(String ticketsId) {
        int audit = ticketsMapper.selectAuditByTicketsId(ticketsId);
        if (audit==0){
            ticketsMapper.updateOnAudit(ticketsId);
            return AjaxResult.success();
        }else {
            List<Bicycle> bicycles = bicycleMapper.selectBicycleListByTicketsId(ticketsId);
            if (bicycles.size()==0){
                 ticketsMapper.updateUpAudit(ticketsId);
            }
            return AjaxResult.success();
        }
    }
    /**
     * 结案/反结案生产工单
     *
     * @param ticketsId 生产工单主键
     * @return 结果
     */
    @Override
    public AjaxResult updateClose(String ticketsId) {
        Tickets tickets = ticketsMapper.selectTicketsByTicketsId(ticketsId);
        if (tickets.getClose()==0){
            ticketsMapper.updateOnClose(ticketsId);
            return AjaxResult.success();
        }else {
            if (tickets.getQuantityStorage()==tickets.getPlannedQuantity()){
                return AjaxResult.error();
            }
            ticketsMapper.updateUpClose(ticketsId);
            return AjaxResult.success();
        }
    }

    @Override
    public AjaxResult selectUnPrintNumberByTicketsId(String ticketsId) {
        int PrintNumber=bicycleMapper.selectNumberByTicketsId(ticketsId);
        Tickets tickets = ticketsMapper.selectTicketsByTicketsId(ticketsId);
        Long plannedQuantity = tickets.getPlannedQuantity();
        Long UnPrintNumber=plannedQuantity-PrintNumber;
        return AjaxResult.success(UnPrintNumber);
    }
    /**
     * 生成并打印车辆ID
     *
     * @param ticketsId 生产工单主键
     * @return 结果
     */
    @Override
    public AjaxResult addBicycleByticketsId(String ticketsId, Integer printNumber) {
        Tickets tickets = ticketsMapper.selectTicketsByTicketsId(ticketsId);
        for (int i=0;i<printNumber;i++){
            Bicycle bicycle = new Bicycle();
            bicycle.setOrderId(ticketsId);
            String bicycleId = ruleSettingService.genBikeId(Bicycle.class);
            bicycle.setBicycleId(bicycleId);
            bicycle.setAudit(0L);
            bicycle.setClientOrderNumber(tickets.getClientOrderNumber());
            bicycle.setProductId(tickets.getProductId());
            bicycle.setProductName(tickets.getProductName());
            bicycle.setSpecifications(tickets.getSpecifications());
            bicycle.setClientName(tickets.getClientName());
            bicycle.setColor(tickets.getColour());
            bicycle.setClinentOrderDate(tickets.getClinentOrderDate());
            bicycle.setWarehouseId(tickets.getWarehouseId());
            bicycle.setStatus(0L);
            bicycle.setType(2L);
            bicycle.setStatus(0L);
            bicycle.setOrderFlag(0L);
            bicycle.setControllerFlag(0L);
            bicycle.setPackagingFlag(0L);
            System.out.println(bicycle);
            int res = bicycleMapper.insertBicycle(bicycle);
        }

        return AjaxResult.success();
    }
    /**
     * 查询车辆ID
     *
     * @param ticketsId 生产工单主键
     * @return 结果
     */
    @Override
    public List<Bicycle> selectBicycleByTicketsId(String ticketsId) {

        return bicycleMapper.selectBicycleListByTicketsId(ticketsId);
    }

    /**
     * 分配产品线
     * @param ticketsIds
     * @param productLine
     * @return
     */
    @Override
    public AjaxResult updateTicketsByproductLine(ArrayList<String> ticketsIds, String productLine) {
        for (String ticketsId:ticketsIds) {
            Tickets tickets = new Tickets();
            tickets.setTicketsId(ticketsId);
            tickets.setProductLine(productLine);
            ticketsMapper.updateTickets(tickets);
        }
        return AjaxResult.success();
    }

    /**
     * 手工补打车辆ID
     * @param ticketsId
     * @param bicycleId
     * @return
     */
    @Override
    public int insertBicycle(String ticketsId,String bicycleId) {
        Tickets tickets = ticketsMapper.selectTicketsByTicketsId(ticketsId);
        Bicycle bicycle = new Bicycle();
        bicycle.setBicycleId(bicycleId);
        bicycle.setOrderId(ticketsId);
        bicycle.setClientOrderNumber(tickets.getClientOrderNumber());
        bicycle.setClinentOrderDate(tickets.getClinentOrderDate());
        bicycle.setProductId(tickets.getProductId());
        bicycle.setProductName(tickets.getProductName());
        bicycle.setSpecifications(tickets.getSpecifications());
        bicycle.setClientName(tickets.getClientName());
        bicycle.setColor(tickets.getColour());
        bicycle.setWarehouseId(tickets.getWarehouseId());
        bicycle.setAudit(0L);
        bicycle.setOrderFlag(0L);
        bicycle.setControllerFlag(0L);
        bicycle.setPackagingFlag(0L);
        bicycle.setStatus(0L);
        return bicycleMapper.insertBicycle(bicycle);
    }
}
